<?php
class TiepnhanController extends AppController {
	public $uses = array('Hoso','Tthc','ThanhPhan','ThanhPho','ThanhPhanTthc','Status','Attachment', 'LichsuHoso');
	
	public function beforeFilter() {
		$this->layout = 'quanly';
	}
	
	public function index($status = null) {
		$st = array(
			$this->Status->CHO_GOI,
			$this->Status->HS_HEN_TIEP_NHAN_HS_GOC,
			$this->Status->KHONG_CO_MAT, 
			$this->Status->HS_KHONG_HOP_LE,
			$this->Status->HS_HOP_LE);
		// debug($status);
		// debug(!empty($status) && in_array($status, $st));
		if (!empty($status) && in_array($status, $st)) {
			$st = array($status);
		}
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array(
				'Hoso.trang_thai' => $st
			),'order' => array('Hoso.ngay_dang_ky' => 'asc')
		));
		
		$listStatus = array(
			1 => 'All',
			$this->Status->CHO_GOI => 'Chờ gọi',
			$this->Status->HS_HEN_TIEP_NHAN_HS_GOC => 'Hẹn tiếp nhận hồ sơ gốc',
			$this->Status->KHONG_CO_MAT => 'Gọi không có mặt', 
			$this->Status->HS_KHONG_HOP_LE => 'Hồ sơ không hợp lệ',
			$this->Status->HS_HOP_LE => 'Hồ sơ đầy đủ');
			
		$this->set('listStatus', $listStatus);
		$this->set('status', $status);
		
		$this->set('listHosos', $listHosos);
	}
	
	public function tiepnhan ($id = null) {
		$hoso = $this->Hoso->findById($id);
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		if (!empty($hoso)){
			$thanhPhans = $this->ThanhPhan->getListThanhPhanGiayToByTthcId($hoso['Tthc']['id']);
			$this->set("thanhPhans", $thanhPhans);
			
			$attachments = $this->Attachment->find("all", array('conditions' => array("hoso_id" => $id)));
			$this->set("attachments", $attachments);
		} else {
			$this->log("Invalid document Id:"+$id);
		}
		
		$hoso['Hoso']['ngay_tra'] = empty($hoso['Hoso']['ngay_hen_tra'])?"":CakeTime::format('d/m/Y',$hoso['Hoso']['ngay_hen_tra']);
		
		$this->request->data['Hoso']['ghi_chu'] = $hoso['Hoso']['ghi_chu'];
		$this->set("hoso", $hoso);
	}

	/**
	 * Xoa ho so dang ky , vi khong hop le 
	 */
	public function delete ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_DANGKY_KHONG_HOP_LE);
			$this->Hoso->save();
		} else {
			$this->log("Deleting document: invalid document id= "+$id);
		}
		
		$this->redirect('index');
	}
	
	/**
	 * tra lai ho so dang ky, vi khong hop le 
	 */
	public function reject ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_KHONG_HOP_LE);
			$this->Hoso->save();
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		
		$this->redirect('index');
	}
	
	public function phanxuly ($id = null) {
		if (!empty($this->request->params['pass'][0])) {
			$selections = split(",",$this->request->params['pass'][0]."-1");
			foreach ($selections as $key => $id) {
				$this->Hoso->recursive = -1;
				$hoso = $this->Hoso->findById($id);
				if (!empty($hoso) && $hoso['Hoso']['trang_thai'] == $this->Status->HS_HOP_LE) {
					$date1 = new DateTime();
					$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
					
					$this->Hoso->id= $id;
					$this->Hoso->saveField('trang_thai', $this->Status->CHO_PHAN_XULY);
					//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
					$this->Hoso->save();
					
					// update history
					
					$his = array(); $his['LichsuHoso'] = array();
					$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
					$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
					$his['LichsuHoso']['status'] = $this->Status->CHO_PHAN_XULY;
					$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Chuyển phân xử lý';
					$his['LichsuHoso']['thao_tac'] = 'Chuyển phân xử lý';
					$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
					
					$this->LichsuHoso->create();
					$this->LichsuHoso->save($his);
				}
			}
		} else {
			$this->log("Rejecting document: invalid document id= "+$id);
		}
		// $this->render('/index.ctp');
		$this->redirect('index');
	}
	
	public function duyetHoso ($id = null) {
		if (!empty($this->request->data)) {
			$date = !empty($this->request->data['Hoso']['ngay_tra'])?$this->convertStrDate($this->request->data['Hoso']['ngay_tra']): NULL;
			$date1 = new DateTime();
			$date2 = new DateTime($date);
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_HOP_LE);
			$this->Hoso->saveField('ghi_chu', $this->request->data['Hoso']['ghi_chu']);
			//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
			
			!empty($date)?$this->Hoso->saveField('ngay_hen_tra', $date):'';
			
			$this->Hoso->save();
			
			// update history
			
			$his = array(); $his['LichsuHoso'] = array();
			
			$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
			$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
			$his['LichsuHoso']['status'] = $this->Status->HS_HOP_LE;
			$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Duyệt hồ sơ';
			$his['LichsuHoso']['thao_tac'] = 'Duyệt hồ sơ';
			$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
			
			$this->LichsuHoso->create();
			$this->LichsuHoso->save($his);
			
			$this->redirect("index");
		}
	}
	
	public function khongcomat ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->KHONG_CO_MAT);
			$this->Hoso->saveField('ghi_chu', $this->request->data['Hoso']['ghi_chu']);
			//$this->Hoso->saveField('ngay_hen_gap', $date);
			$this->Hoso->save();
			
			// update history
			$date1 = new DateTime();
			//$date2 = new DateTime($date);
			
			$his = array(); $his['LichsuHoso'] = array();
			
			$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
			$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
			$his['LichsuHoso']['status'] = $this->Status->KHONG_CO_MAT;
			$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Duyệt hồ sơ';
			$his['LichsuHoso']['thao_tac'] = 'Duyệt hồ sơ';
			//$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
			
			$this->LichsuHoso->create();
			$this->LichsuHoso->save($his);
			
			$this->redirect("index");
		}
	}
	
	public function phieuhen () {
	}
	
	public function back () {
		$this->redirect('index');
	}
}
